package com.cy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.bfzcommons.mapper.CategoryMapper;
import com.cy.bfzcommons.mapper.PictureMapper;
import com.cy.bfzcommons.mapper.ProductMapper;
import com.cy.bfzcommons.pojo.Category;
import com.cy.bfzcommons.pojo.Picture;
import com.cy.bfzcommons.pojo.Product;
import com.cy.bfzcommons.service.ProductService;
import com.cy.bfzcommons.web.util.PageUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    @Transactional
    public void saveProduct(Product product) {
        productMapper.insert(product);
        List<Picture> list = product.getPictures();
        for (Picture picture : list){
            picture.setProductId(product.getProductId());
            pictureMapper.insert(picture);
        }
    }

    @Override
    public List<Product> getProduct() {
//        List<Product> list = productMapper.selectList(null);
//        return list;
            List<Product> list = productMapper.findProduct();
            return list;
    }

    /**
     * 根据id查询商品   返回商品的全部图片
     * @param id
     * @return
     */
    @Override
    public Product productById(Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        Product product = productMapper.selectById(id);
        QueryWrapper<Picture> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("product_id",id);
        product.setPictures(pictureMapper.selectList(queryWrapper1));
        QueryWrapper<Category> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("name");
        product.setCategoryName(categoryMapper.selectNameById(product.getProductId()));
        return product;
    }

    /**
     * 模糊查询商品  根据商品的名字和详情进行查询   不返回详细的图片
     * @param describe
     * @return
     */
    @Override
    public List<Product> productLike(String describe) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",describe).or().like("introduct",describe);
        List<Product> list =  productMapper.selectList(queryWrapper);

        return list;
    }

    /**
     * 修改商品的信息 还有商品详情图片
     * @param product
     */
    @Override
    @Transactional
    public void updateProduct(Product product) {
        productMapper.updateById(product);
        List<Picture> list = product.getPictures();
//        if (list != null) {
//            for (Picture picture : list) {
//                pictureMapper.updateById(picture);
//            }
//        }
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Product_id",product.getProductId());
        pictureMapper.delete(queryWrapper);

        List<Picture> pictures = product.getPictures();
         if (pictures!=null) {
            for (Picture picture : pictures) {
                picture.setProductId(product.getProductId());
                pictureMapper.insert(picture);
            }
        }
    }

    @Override
    public List<Product> selectProduct(String describe) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", 1).and(!StringUtil.isEmpty(describe), queryWrapper1->queryWrapper1.like("name", describe)).and(!StringUtil.isEmpty(describe), queryWrapper1->queryWrapper1.like("introduct", describe));
        List<Product> list = productMapper.selectList(queryWrapper);
        return list;
    }

//-------------------------------------下方请求将用于前端页面--------------------------------------------

    //有待优化
    @Override
    public List<Product> categorySelect(Integer categoryId) {
        QueryWrapper<Category> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parent", categoryId);
        List<Category> categorys = categoryMapper.selectList(queryWrapper1);
        List<Product> list = new ArrayList<>();
        for (Category category : categorys){
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", category.getCategoryId());
            queryWrapper.eq("state", 1);
            List<Product> products = productMapper.selectList(queryWrapper);
            for (Product product:products){
                list.add(product);
            }
        }
        System.out.println(list.toString());
        return list;
    }

    @Override
    public List<Product> selectByUserId(Integer userId,Boolean state) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("state", state);
        List<Product> list = productMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    @Transactional
    public void insertProduct(Product product) {
        productMapper.insert(product);
        String[] list = product.getPicture();
        for (String pictureString : list){
            Picture picture = new Picture();
            picture.setProductId(product.getProductId());
            picture.setPicture(pictureString);
            pictureMapper.insert(picture);
        }
    }

    /**
     * 根据id查询商品   返回商品的全部图片
     * 曾加商品分类的父级
     * @param id
     * @return
     */
    @Override
    public Product VueProductById(Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        Product product = productMapper.selectById(id);
        QueryWrapper<Picture> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("product_id",id);
        product.setPictures(pictureMapper.selectList(queryWrapper1));
        QueryWrapper<Category> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("name");
        product.setCategoryName(categoryMapper.selectNameById(product.getCategoryId()));

        product.setParent(categoryMapper.VueSelectNameById(product.getCategoryId()));
        System.out.println("categoryname:"+product);
        return product;
    }

    /**
     * 修改商品的信息 还有商品详情图片
     * @param product
     */
    @Override
    @Transactional
    public void VueUpdateProduct(Product product) {
        productMapper.updateById(product);
        //List<Picture> list = product.getPictures();
//        if (list != null) {
//            for (Picture picture : list) {
//                pictureMapper.updateById(picture);
//            }
//        }
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Product_id",product.getProductId());
        pictureMapper.delete(queryWrapper);

        String[] pictures = product.getPicture();
        if (pictures!=null) {
            for (String picture : pictures) {
                Picture picture1 = new Picture();
                picture1.setProductId(product.getProductId());
                picture1.setPicture(picture);
                pictureMapper.insert(picture1);
            }
        }
    }

    @Transactional
    @Override
    public Object productLike2(String describe,String category,Integer min,Integer max) {
        Integer integer = null;
        if(category!=null){

            integer = categoryMapper.selectIdByName(category);
        }
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(describe!=null,"name",describe)
                .eq(integer!=null,"category_id",integer)
                .between("price", min,max);
        List<Product> list =  productMapper.selectList(queryWrapper);
        System.out.println(list+"********************************");
        PageInfo<Object> objectPageInfo = PageUtil.startpage().doSelectPageInfo(() ->
                productMapper.selectList(queryWrapper));

        return objectPageInfo;

    }

    @Override
    public void updateState(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public List<Product> vueGetProduct(Boolean state) {
//        List<Product> list = productMapper.selectList(null);
//        return list;
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", state);
        List<Product> list = productMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void delete(Integer productId) {
        productMapper.deleteById(productId);
    }
}
